<?php

namespace Index\Controller;

use Think\Controller;

class CommonController extends Controller {
	public function _initialize() {
		if(session('uid')==null){
			$this->error('没有登录！',__APP__.'/Index/Login/index');
		}
		$username=I('session.username');
		$this->username = $username;
		
		$this->GetMenuList(__SELF__);
		$this->GetBreadMenu(__SELF__);
	}
	public function _empty() {
	 	$this->display();
	}
	
	/**
	 * 快速排序
	 * @param array
	 * @return array
	**/
	function quickSort($array)
	{
		if(!isset($array[1]))
			return $array;
		$mid = $array[0]; //获取一个用于分割的关键字，一般是首个元素
		$leftArray = array();
		$rightArray = array();
		foreach($array as $v)
		{
			if($v > $mid)
				$rightArray[] = $v;  //把比$mid大的数放到一个数组里
			if($v < $mid)
				$leftArray[] = $v;   //把比$mid小的数放到另一个数组里
		}
		$leftArray = quickSort($leftArray); //把比较小的数组再一次进行分割
		$leftArray[] = $mid;        //把分割的元素加到小的数组后面，不能忘了它哦
		$rightArray = quickSort($rightArray);  //把比较大的数组再一次进行分割
		return array_merge($leftArray,$rightArray);  //组合两个结果
	}
	
	/**
	 * 验证是否是本用户
	 * @param $unit:表的字段
	 * @param $value:值
	 * @param $table:表名
	 * @return boolean
	 */
	public function isMe($table){
		
	}
	
	/**
	 * 验证用户是否是管理员
	 * @return boolen
	 */
	public function isAdmin(){
		$data = M('user');
		$username = I('session.username');
		$id = I('session.uid');
		$where = array(
			'id' => $id,
			'username' => $username
		);
		
		$res1 = $data->where($where)->find();
		//echo $data->_sql();
		if($res1['group']==1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 验证字段是否重复
	 * @param $unit:表的字段
	 * @param $value:值
	 * @param $table:表名
	 * @return boolean
	 */
	public function isSame($unit,$value,$table,$guid=''){
		$sql = M($table);
		if($guid!=''){
			$where = array(
				'guid'	=>	$guid,
				$unit 	=> 	$value
			);
		}else{
			$where = array(
					$unit 	=> 	$value
			);
		}
		$data = $sql ->where($where)-> select();
		if(!$data){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 生成GUID
	 * @return string
	 */
	function create_guid() {
		$charid = strtoupper(md5(uniqid(mt_rand(), true)));
		$hyphen = chr(45);// "-"
		//$uuid = chr(123)// "{"
		$uuid = substr($charid, 0, 8).$hyphen
		.substr($charid, 8, 4).$hyphen
		.substr($charid,12, 4).$hyphen
		.substr($charid,16, 4).$hyphen
		.substr($charid,20,12);
		//.chr(125);// "}"
		return $uuid;
	}
	
	/**
	 * 获取日志数据库
	 * @return type
	 */
	public function logs(){
	
		$log = M('logs');
		$data = array(
				'guid' => '2222',
				'app'	=>	'admin',
		);
		$log -> add($data);
	}
	
	
	/**
	 * 生成面包屑菜单
	 */
	public function GetBreadMenu($url){
		//以"/"获取地址栏中的url，存为数组
		$catidList = explode("index.php",$url);
		$catList = explode("/", $catidList[1]);

		$catName = $catList[2];
		$actionName = $catList[3];	//Action为  "/$catName/$actionName"
		$action  = "/" . $catName ."/" . $actionName; 
				
		$sql = M('menu');
		$where['url'] = array('like','%'.$catName.'%');
		$data = $sql->where($where)->select();
		$this->assign ( 'bread', $data );
		//var_dump($data);
		//echo $sql->_sql();
	}
	
	/**
	 * 菜单管理
	 * 
	 */
	public function GetMenuList($url){
		//以"/"获取地址栏中的url，存为数组
		$catidList = explode("index.php",$url);
		$catList = explode("/", $catidList[1]);

		$catName = $catList[2];
		$action = '/' . $catList[2] . '/' . $catList[3];
				
		$sql = M('menu');
		
		$data = $sql->select();
		for($i=0;$i<count($data);$i++){
			echo $data[$i]['url'];
			if($data[$i]['url']==$action){
				$data[$i]['active']="active";
			}else{
				$data[$i]['active']="aa";
			}
		}
		//var_dump($data);
		//var_dump($action);
		$this->assign ( 'menu', $data );
	}
	
}